<?php
/**
 * Date: 2018/3/1
 * Time: 17:34
 * Author: zeronlee
 */

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Auth\Sso\Tool;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Auth\Sso\STD3Des;
use App\Http\Controllers\Auth\Sso\Http;
use App\Models\User\Users;
use Illuminate\Support\Facades\Cookie;

class SsoController extends Controller
{

    public function setcookies (Request $request) {
        // $des = new STD3Des(base64_encode(env('SSO_APPKEY')));
        $des = new STD3Des(base64_encode(env('SSO_APPKEY')));

        $info = $des->decrypt(base64_decode($request->ppinf));
        \Log::info('解密后的回掉参数'.$info);
        // cookie('ppinf', $request->ppinf, 30 * 60);
        $request->session()->put('desPpinf', $info);
        // \Log::info('解密后的session'.$request->session()->pull('desPpinf'));
        // \Log::info('解密后的session，get方法读取'.$request->session()->get('desPpinf'));
        session('desPpinf',$info);
        $request->session()->save();
        //set cookies
        /*$ssoParam = ['ppinf' => $request->ppinf, 'ppdig' => $request->ppdig];
        \Log::info('setcookies设置的session'.$request->session()->pull('desPpinf'));
        return response('')->cookie('ssoParam', json_encode($ssoParam), 8 * 3600);*/
    }

    public function success (Request $request) {
        $info = $request->session()->get('desPpinf');

        \Log::info('登陆回掉session信息'.json_encode($info));
        $info = session('desPpinf');
        \Log::info('session非request'.json_encode($info));
        // $decodeInfo = explode('|', $info);
        //获取用户信息
        // [sso-host]/passport-sso/sso-v1/getUserInfo.do
        $url = env('SSO_HOST') . 'passport-sso/sso-v1/getUserInfo.do';
        $tool = new Tool();
        //$timeStamp = $tool->getMillisecond();
        if (!$info){

            $des = new STD3Des(base64_encode(env('SSO_APPKEY')));
            $ssoParam = json_decode($request->cookie('ssoParam'), true);
            \Log::info('客户端cookie'.$request->cookie('ssoParam'));
            $info = $des->decrypt($ssoParam['ppinf']);
            if (!is_array($info)){
                \Log::info('登陆回掉cookie信息'.$info);
                \Log::info('登陆回掉setcookie方法设置解密ppinf失败');
            }else{
                \Log::info('登陆回掉cookie信息'.json_encode($info));
            }
        }
        $decodeInfo = explode('|', $info);
        $timeStamp = $decodeInfo[6];
        $ppinf = $decodeInfo[0] . '|' . $decodeInfo[1] . '|' . $timeStamp;
        $signature = md5($ppinf);
        $des = new STD3Des(base64_encode(env('SSO_APPKEY')));
        $secPpinf = $des->encrypt($ppinf);
        //拼写参数header请求
        $header = [
            'appId:' . env('SSO_APPID'),
            'timestamp:' . $timeStamp,
            'ppinf:' . $secPpinf,
            'signature:' . $signature
        ];
        $content = [];
        $http = new Http();
        $response = $tool->toJson($http->tocurl($url, $header, $content));
        \Log::info('登陆接口返回状态'.$response->doneCode);
        if ($response->doneCode == 0) {

            $data = $tool->toJson($des->decrypt($response->data));
            $user = Users::where('name', $data->mobile)->first();
            if (empty($user)) {

                $user = new Users();

                //入库
                $user->saveUser($data);
            }
            //用user id登录
            \Auth::loginUsingId($user->id);
            //redirect index
            $protocol = 'http://';
            if ($request->server('SERVER_PORT') == '443') {
                $protocol = 'https://';
            }
            $host = $request->server('HTTP_HOST');
            return redirect($protocol.$host.'/src/');
        }
        //登陆失败
        return redirect(env('SSO_HOST') . 'sso-login.do?appId=' . env('SSO_APPID'));
    }

    public function clearcookies (Request $request) {
        if (\Auth::user()) {
            //退出登陆
            \Auth::logout();
        }
        //删除ppinf和ppdig
        return response('')->cookie('ssoParam', '', -1);
    }

    public function logout (Request $request) {
        if (\Auth::user()) {

            $appId = env('SSO_APPID');
            $ssoParam = json_decode($request->cookie('ssoParam'), true);
            $ppinf = $ssoParam['ppinf'];
            $ppdig = $ssoParam['ppdig'];
            $url = env('SSO_HOST') . "/sso-v1/logout.do";

            //String success = "http://localhost:8080/passport-demo/thirdParty/sso-v1/clearcookies.do?ppinf=" + ppinf + "&ppdig=" + ppdig;
            $success = env('APP_URL');
            $param = "?appId=" . $appId . "&success=" . $success . "&ppinf=" . $ppinf . "&ppdig=" . $ppdig;

            //重定向到SSO
            return redirect($url . $param);
        }
        //跳转登陆
        return redirect(env('SSO_HOST') . 'sso-login.do?appId=' . env('SSO_APPID'));
    }
}